package com.xxx.blog.web.admin;

import com.xxx.blog.po.Comment;
import com.xxx.blog.po.Reply;
import com.xxx.blog.service.CommentService;
import com.xxx.blog.service.ReplyService;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class CommentsController {
    @Resource
    private CommentService commentService;
    @Resource
    private ReplyService replyService;

    @GetMapping("/comments")
    public String comments(@PageableDefault(size = 5, sort = {"id"}, direction = Sort.Direction.DESC) Pageable pageable, Model model) {
        model.addAttribute("page", commentService.adminListComment(pageable));
        return "admin/index";
    }

    @GetMapping("/comments/{id}/delete")
    public String delete(@PathVariable Long id, RedirectAttributes attributes) {
//        List<Reply> list=new ArrayList<Reply>();
//        list= replyService.findById(id);
//        System.out.println(list.size());
//        System.out.println(list.get(0).getComment_id());
//        if(list.size()!=0){
//            for (int i=0;i<list.size();i++){
//                replyService.deleteById(list.get(i).getComment_id());
//            }
//
//        }
//        commentService.deleteById(id);
        attributes.addFlashAttribute("message","删除功能暂未开放");
//        commentService.deleteTag(id);
//        attributes.addFlashAttribute("message","删除成功");
        return "redirect:/admin/comments";
    }
}
